﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DDMedlem.Buisness.Rapportering;
using System.Data;
namespace DDMedlem
{
    public partial class rapport : System.Web.UI.Page
    {
        private static DataSet dset;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (dset == null)
            {
                Export_Excel_Btn.Visible = false;
            }
            

            if (!IsPostBack)
            {
                Rapport r = new Rapport();
                Liste_DropDown_List.DataSource = r.QueryListe();

                Liste_DropDown_List.DataBind();
            }
           // Rapport r = new Rapport();
           // Rapport.findMetode[] f = { r.VisAlleMedlemmer };
           //DataSet d = r.HentListe(f[0]);
        }

        protected void HentListe_Btn_Click(object sender, EventArgs e)
        {
            Export_Excel_Btn.Visible = true;
            Liste_GridView.DataSource = null;
            Liste_GridView.DataBind();
            
            Rapport r = new Rapport();
           

            
            if(Liste_DropDown_List.SelectedIndex > 0)
            {
                r = new Rapport();
                DataSet ds = r.HentListe(r.QueryListe()[Liste_DropDown_List.SelectedIndex].metode, r.QueryListe()[Liste_DropDown_List.SelectedIndex].SQLstring);
                dset = ds;
                Liste_GridView.DataSource = ds;
                Liste_GridView.DataBind();

            }
            else if (Liste_DropDown_List.SelectedIndex == 0)
            {
                Export_Excel_Btn.Visible = false;
                dset = null;
            }
            
            
        }

        protected void Export_Excel_Btn_Click(object sender, ImageClickEventArgs e)
        {
            
            if (dset != null)
            {
                Data.Export.ExporterListe(dset);



                Response.ContentType = "image/csv";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Session["username"].ToString() + ".csv");
                Response.TransmitFile(Server.MapPath("~/export/" + HttpContext.Current.Session["username"] + ".csv"));
                Response.End();
            }
        }

        protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
        {

           
           

            if (dset.Tables[0] != null)
            {

                //Sort the data.
               
                dset.Tables[0].DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection);
                Liste_GridView.DataSource = dset;
                
                Liste_GridView.DataBind();

               
            }

        }

        private string GetSortDirection(SortDirection s)
        {
            if (s == SortDirection.Ascending)
                return "ASC";
            else return "DESC";

        }

        


    }
}